package com.lfg.leetcode.leetcode0;

public class No82deleteDuplicates {

    public static void main(String[] args) {
        ListNode ret = deleteDuplicates(ListNode.deserialize("[4,4,5]"));
        while (ret != null) {
            System.out.println("[" + ret.val + "]");
            ret = ret.next;
        }
    }

    public static ListNode deleteDuplicates(ListNode head) {
        if (null == head) {
            return null;
        }
        ListNode ret = new ListNode(1);
        ListNode retNext = ret;
        ListNode duplicate = null;

        ListNode next = head.next;
        ListNode current = head;
        while (current != null) {
            next = current.next;
            if (next != null && current.val == next.val) {
                duplicate = current;
                current = next.next;
            } else {
                retNext.next = current;
                retNext = retNext.next;

                current = current.next;
            }
            while (duplicate != null && current != null) {
                if (duplicate.val != current.val) {
                    duplicate = null;
                } else {
                    current = current.next;
                }
            }
        }
        retNext.next = null;
        return ret.next;
    }

}

